package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.bean.subscribemsg.CategoryData;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateKeyword;
import me.chanjar.weixin.common.bean.subscribemsg.PubTemplateTitleListResult;
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <h3>springcloudSaas</h3>
 * <p>订阅通知</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-15 23:23
 **/
@RestController
@RequestMapping("/subscribeMsg")
public class MpSubscribeMsgController {
    @Autowired
    private WxMpService wxService;
    @Autowired
    private IMpAccountService iMpAccountService;

    /**
     * 组合模板并添加至帐号下的个人模板库
     */
    @RequiresPermissions("mp:subscribeMsg:addTemplate")
    @Log(title = "组合模板并添加至帐号下的个人模板库 ", businessType = BusinessType.OTHER)
    @PutMapping("/addTemplate/{id}")
    public AjaxResult addTemplate(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String ids = "1";
        List<Integer> keywordIdList = Arrays.asList(1, 4);
        String sceneDesc = "场景描述";
        return AjaxResult.success(wxService.switchoverTo(appId).getSubscribeMsgService().addTemplate(ids, keywordIdList, sceneDesc));
    }


    /**
     * 删除帐号下的某个模板
     */
    @RequiresPermissions("mp:subscribeMsg:delTemplate")
    @Log(title = "删除帐号下的某个模板  ", businessType = BusinessType.OTHER)
    @GetMapping("/delTemplate/{id}")
    public AjaxResult delTemplate(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String templateId = "1";
        return AjaxResult.success(wxService.switchoverTo(appId).getSubscribeMsgService().delTemplate(templateId ));
    }


    /**
     * 获取公众号类目
     */
    @RequiresPermissions("mp:subscribeMsg:getCategory")
    @Log(title = "获取公众号类目", businessType = BusinessType.OTHER)
    @GetMapping("/getCategory/{id}")
    public AjaxResult getCategory(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        List<CategoryData> categoryDataList = wxService.switchoverTo(appId).getSubscribeMsgService().getCategory();
        return AjaxResult.success(categoryDataList);
    }


    /**
     * 获取模板库某个模板标题下关键词库
     */
    @RequiresPermissions("mp:subscribeMsg:getPubTemplateKeyWordsById")
    @Log(title = "获取模板库某个模板标题下关键词库 ", businessType = BusinessType.OTHER)
    @GetMapping("/getPubTemplateKeyWordsById/{id}")
    public AjaxResult getPubTemplateKeyWordsById(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String templateId  = "1";
        List<PubTemplateKeyword> pubTemplateKeywordList = wxService.switchoverTo(appId).getSubscribeMsgService().getPubTemplateKeyWordsById(templateId);
        return AjaxResult.success(pubTemplateKeywordList);
    }

    /**
     * 获取帐号所属类目下的公共模板标题
     */
    @RequiresPermissions("mp:subscribeMsg:getPubTemplateTitleList")
    @Log(title = "获取帐号所属类目下的公共模板标题 ", businessType = BusinessType.OTHER)
    @GetMapping("/getPubTemplateTitleList/{id}")
    public AjaxResult 	getPubTemplateTitleList(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String[] ids = {"1"};
        int start = 0;
        int limit = 10;
        PubTemplateTitleListResult pubTemplateTitleListResult = wxService.switchoverTo(appId).getSubscribeMsgService().getPubTemplateTitleList(ids, start, limit);
        return AjaxResult.success(pubTemplateTitleListResult);
    }


    /**
     * 获取当前帐号下的个人模板列表
     */
    @RequiresPermissions("mp:subscribeMsg:getTemplateList")
    @Log(title = "获取当前帐号下的个人模板列表  ", businessType = BusinessType.OTHER)
    @GetMapping("/getTemplateList/{id}")
    public AjaxResult getTemplateList(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        List<TemplateInfo> templateList = wxService.switchoverTo(appId).getSubscribeMsgService().getTemplateList();
        return AjaxResult.success(templateList);
    }

    /**
     * 发送订阅消息
     */
    @RequiresPermissions("mp:subscribeMsg:send")
    @Log(title = "发送订阅消息", businessType = BusinessType.OTHER)
    @PostMapping("/send/{id}")
    public AjaxResult send(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpSubscribeMessage subscribeMessage = new WxMpSubscribeMessage();
        String subscribeId = wxService.switchoverTo(appId).getSubscribeMsgService().send(subscribeMessage);
        return AjaxResult.success(subscribeId);
    }

    /**
     * 发送一次性订阅消息
     */
    @RequiresPermissions("mp:subscribeMsg:sendOnce")
    @Log(title = "发送一次性订阅消息 ", businessType = BusinessType.OTHER)
    @PostMapping("/sendOnce/{id}")
    public AjaxResult sendOnce(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpSubscribeMessage subscribeMessage = new WxMpSubscribeMessage();
        boolean subscribeId = wxService.switchoverTo(appId).getSubscribeMsgService().sendOnce(subscribeMessage);
        return AjaxResult.success(subscribeId);
    }

    /**
     * 构造用户订阅一条模板消息授权的url连接
     */
    @RequiresPermissions("mp:subscribeMsg:subscribeMsgAuthorizationUrl")
    @Log(title = "构造用户订阅一条模板消息授权的url连接  ", businessType = BusinessType.OTHER)
    @PostMapping("/subscribeMsgAuthorizationUrl/{id}")
    public AjaxResult subscribeMsgAuthorizationUrl(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String redirectURI = "http://www.baidu.com";
        int scene = 1;
        String reserved = "xxx";
        String subscribeId = wxService.switchoverTo(appId).getSubscribeMsgService().subscribeMsgAuthorizationUrl(redirectURI, scene, reserved);
        return AjaxResult.success(subscribeId);
    }
}


